#include "windowManager.h"




windowManager::windowManager() {

    msg = NULL;
    net = NULL;

    mouseX = 0;
    mouseY = 0;

    WindowCNT = 0;

    WindowMove=-1;
    WindowResize=-1;

    WindowTempX = 0;
    WindowTempY = 0;
    WindowFocusMove = 0;

    runningHandleNumber = 0;

    debug = 0;

    setupFontW = 8;
    setupFontH = 10;

    myGrafics = new grafics;


}



windowManager::~windowManager() {
    delete myGrafics;
}



bool windowManager::isPlainChar(int pAsciiCode) {

    if( (pAsciiCode>=65 && pAsciiCode<=90) ||       // A-Z
        (pAsciiCode>=97 && pAsciiCode<=122) ||      // a-z
        (pAsciiCode>=48 && pAsciiCode<=57) ||       // 0-9
         pAsciiCode==95 ||                          // _
         pAsciiCode==46 ||                          // .
         pAsciiCode==32 ||                          // SPACE
         pAsciiCode==45 ) return true;              // -
    else return false;

}


string windowManager::breakLines(string pText, int w) {

    string retVal;
    int charPerLine=0;
    int charCnt=0;
    char tempChar;
    int cnt=0;

    charPerLine = w / 8;

    //sretVal = "Lines:" + boost::lexical_cast<std::string>(charPerLine) + "#" + retVal;
    //if(strstr(&tempChar, "#")) {

    for(charCnt=0;charCnt<(int)pText.length();charCnt++) {
        tempChar = pText.at(charCnt);
        if(cnt>=charPerLine || strcmp(&tempChar, "#") == 0 ) {

            if( strcmp(&tempChar, " ") != 0 ) cnt=0;
            else cnt = -1;

            retVal = retVal + "#";
        }
        cnt++;
        retVal = retVal + tempChar;
    }
    return retVal;
}


void windowManager::loadXML(void) {

    int handle;
    windows tmpWindow;


    // Das nächste object fuscht in den speicher hinnein


    // this open and parse the XML file:
    XMLNode xMainNode=XMLNode::openFileHelper("config/windows.xml","PMML" );
    XMLNode xNode=xMainNode.getChildNode("windows");
    XMLNode tmpNode;


    // count
    int btnCount = 0;
    int txtCount = 0;
    int edtCount = 0;
    int imgCount = 0;


    int winCount = xNode.nChildNode("window");
    for(int i=0; i<winCount && i<1000; i++) {

        tmpNode = xNode.getChildNode("window",i);

        tmpWindow.id        = atoi( tmpNode.getAttribute("id") );
        tmpWindow.aktiv     = atoi( tmpNode.getAttribute("aktiv") );
        tmpWindow.visible   = atoi( tmpNode.getAttribute("visible" ));
        tmpWindow.design    = atoi( tmpNode.getAttribute("design") );
        tmpWindow.resizable = atoi( tmpNode.getAttribute("resizable") );
        tmpWindow.movable   = atoi( tmpNode.getAttribute("movable") );
        tmpWindow.menue     = atoi( tmpNode.getAttribute("menue") );
        tmpWindow.x         = atoi( tmpNode.getAttribute("x") );
        tmpWindow.y         = atoi( tmpNode.getAttribute("y") );
        tmpWindow.w         = atoi( tmpNode.getAttribute("w") );
        tmpWindow.h         = atoi( tmpNode.getAttribute("h") );




        //buttons
        btnCount = tmpNode.nChildNode("button");
        tmpWindow.buttonCount = btnCount;
        for(int e=0; e<btnCount && e<1000; e++) {
            if(tmpNode.getChildNode("button",e).getAttribute("lable") != NULL) {
                tmpWindow.button[e].handle  = atoi( tmpNode.getChildNode("button",e).getAttribute("handle") );
                tmpWindow.button[e].aktiv   = atoi( tmpNode.getChildNode("button",e).getAttribute("aktiv") );
                tmpWindow.button[e].design  = atoi( tmpNode.getChildNode("button",e).getAttribute("design") );
                tmpWindow.button[e].visible = atoi( tmpNode.getChildNode("button",e).getAttribute("visible") );
                tmpWindow.button[e].x       = atoi( tmpNode.getChildNode("button",e).getAttribute("x") );
                tmpWindow.button[e].y       = atoi( tmpNode.getChildNode("button",e).getAttribute("y") );
                tmpWindow.button[e].w       = atoi( tmpNode.getChildNode("button",e).getAttribute("w") );
                tmpWindow.button[e].h       = atoi( tmpNode.getChildNode("button",e).getAttribute("h") );
                tmpWindow.button[e].align   = atoi( tmpNode.getChildNode("button",e).getAttribute("align") );
                tmpWindow.button[e].lableAlign   = atoi( tmpNode.getChildNode("button",e).getAttribute("lableAlign") );
                sprintf(tmpWindow.button[e].lable,"%s", tmpNode.getChildNode("button",e).getAttribute("lable") );
            }
        }




        //text
        txtCount = tmpNode.nChildNode("text");
        tmpWindow.textCount = txtCount;
        for(int e=0; e<txtCount && e<1000; e++) {
            tmpWindow.text[e].x       = atoi( tmpNode.getChildNode("text",e).getAttribute("x") );
            tmpWindow.text[e].y       = atoi( tmpNode.getChildNode("text",e).getAttribute("y") );
            tmpWindow.text[e].w       = atoi( tmpNode.getChildNode("text",e).getAttribute("w") );
            tmpWindow.text[e].h       = atoi( tmpNode.getChildNode("text",e).getAttribute("h") );
            tmpWindow.text[e].frame   = atoi( tmpNode.getChildNode("text",e).getAttribute("frame") );
            tmpWindow.text[e].content = tmpNode.getChildNode("text",e).getText();
        }


/*

        //image
        imgCount = tmpNode.nChildNode("image");
        tmpWindow.imageCount = imgCount;
        for(int e=0; e<imgCount && e<1000; e++) {
            tmpWindow.image[e].ptrImage   = NULL;
            tmpWindow.image[e].x          = atoi( tmpNode.getChildNode("image",e).getAttribute("x") );
            tmpWindow.image[e].y          = atoi( tmpNode.getChildNode("image",e).getAttribute("y") );
            tmpWindow.image[e].handle     = atoi( tmpNode.getChildNode("image",e).getAttribute("handle") );
            tmpWindow.image[e].clickable  = atoi( tmpNode.getChildNode("image",e).getAttribute("clickable") );
            tmpWindow.image[e].visible    = atoi( tmpNode.getChildNode("image",e).getAttribute("visible") );
            tmpWindow.image[e].ptrImage   = load_bitmap(tmpNode.getChildNode("image",e).getAttribute("source"), NULL);
            tmpWindow.image[e].w          = tmpWindow.image[e].ptrImage->w;
            tmpWindow.image[e].h          = tmpWindow.image[e].ptrImage->h;
            if(!tmpWindow.image[e].ptrImage)  {
               cout << "Could not load XML image: " << tmpNode.getChildNode("image",e).getAttribute("source") << endl;
            }
        }
*/



        //edit
        edtCount = tmpNode.nChildNode("edit");
        tmpWindow.editCount = edtCount;
        for(int e=0; e<edtCount && e<1000; e++) {
            tmpWindow.edit[e].x       = atoi( tmpNode.getChildNode("edit",e).getAttribute("x") );
            tmpWindow.edit[e].y       = atoi( tmpNode.getChildNode("edit",e).getAttribute("y") );
            tmpWindow.edit[e].w       = atoi( tmpNode.getChildNode("edit",e).getAttribute("w") );
            tmpWindow.edit[e].h       = atoi( tmpNode.getChildNode("edit",e).getAttribute("h") );
            tmpWindow.edit[e].aktiv   = atoi( tmpNode.getChildNode("edit",e).getAttribute("aktiv") );
            tmpWindow.edit[e].design  = atoi( tmpNode.getChildNode("edit",e).getAttribute("design") );
            tmpWindow.edit[e].visible = atoi( tmpNode.getChildNode("edit",e).getAttribute("visible") );
            if(tmpNode.getChildNode("edit",e).getText() != NULL) tmpWindow.edit[e].content = tmpNode.getChildNode("edit",e).getText();
            else tmpWindow.edit[e].content = "";
            tmpWindow.edit[e].cursorPos = tmpWindow.edit[e].content.length();
            tmpWindow.edit[e].maxChar = (int)tmpWindow.edit[e].w / 8;

        }
        if(edtCount > 0) tmpWindow.editCursor = 0;
        else tmpWindow.editCursor = -1;






        // Buffer
        tmpWindow.ptrBuffer = create_bitmap(1024, 768);
        if(!tmpWindow.ptrBuffer) msg->Add("loadXML: Couldn't create bitmap!");


        tmpWindow.title = tmpNode.getAttribute("name");

        handle = windowAdd(tmpWindow);

        // If viable set active
        if(tmpWindow.aktiv) windowSetFocus(handle);

    }

}


void windowManager::IntroduceMessage(msgManager *pMsg) {
    msg = pMsg;
    if(debug>0) msg->Add("win: msg linked");
}


void windowManager::IntroduceNetwork(networkManager *pNet) {
    net = pNet;
    if(debug>0) msg->Add("win: net linked");
}




void windowManager::Init(void) {// srNetworkClient *pEngine

//    ptrNetEngine = pEngine;


    //myGrafics->Init(load_bitmap("GFX/mapWindow.bmp", NULL));



    // Load windows
    loadXML();

}



void windowManager::Calculate(void) {

/*
     if(ptrDataCore->todo == 1) {

        Window[windowGetIndexById(8)].aktiv = 1;
        ptrDataCore->todo = 0;
    }
*/

}


void windowManager::RenderEdit(int indexID, int offsetX, int offsetY, int style) {
/*
    int cursor;
    int tmpX, tmpY, tmpW, tmpH;
    string tempStr;

    for(int e=0; e<Window[indexID].editCount; e++) {

        // relative Position
        tmpX = offsetX + Window[indexID].edit[e].x + 5;
        tmpY = offsetY + Window[indexID].edit[e].y + 18;
        tmpW = Window[indexID].edit[e].w;
        tmpH = Window[indexID].edit[e].h;

        tempStr = Window[indexID].edit[e].content;

        // left part
        blit(sfWindow,                  // source BITMAP
             Window[indexID].ptrBuffer, // dest BITMAP
             posWindow[style][14].x,    // source X
             posWindow[style][14].y,    // source Y
             tmpX,                      // dest X
             tmpY,                      // dest Y
             posWindow[style][14].w,    // general W
             posWindow[style][14].h);   // general H


        // middle part
        stretch_blit(sfWindow,                      // source BITMAP
                     Window[indexID].ptrBuffer,     // dest BITMAP
                     posWindow[style][15].x,        // source X
                     posWindow[style][15].y,        // source Y
                     posWindow[style][15].w,        // source W
                     posWindow[style][15].h,        // sourse H
                     tmpX+posWindow[style][14].w,   // dest X
                     tmpY,                          // dest Y
                     tmpW-posWindow[style][16].w,   // dest W
                     posWindow[style][14].h);       // dest H


        // right part
        blit(sfWindow,                  // source BITMAP
             Window[indexID].ptrBuffer, // dest BITMAP
             posWindow[style][16].x,    // source X
             posWindow[style][16].y,    // source Y
             tmpX+tmpW,                 // dest X
             tmpY,                      // dest Y
             posWindow[style][16].w,    // general W
             posWindow[style][16].h);   // general H


        // cursor
        if(Window[indexID].editCursor == e ) {


            // Cursor Position
            cursor = Window[indexID].edit[e].cursorPos*8;


            stretch_blit(sfWindow,                      // source BITMAP
                         Window[indexID].ptrBuffer,     // dest BITMAP
                         posWindow[style][14].x,        // source X
                         posWindow[style][14].y,        // source Y
                         1,        // source W
                         1,        // sourse H
                         tmpX+4+cursor,   // dest X
                         tmpY+2,                          // dest Y
                         1,   // dest W
                         posWindow[style][14].h-4);       // dest H
        }


        // draw content --- Align: Left=0, Centered=1, Right=2
        textprintf_ex(Window[indexID].ptrBuffer, font, tmpX+5, tmpY+3, makecol(0, 0, 0), -1, "%s", tempStr.c_str() );
    }
*/
}



void windowManager::RenderButton(int indexID, int offsetX, int offsetY, int style) {
/*
    int temp;
    int tmpX, tmpY, tmpW, tmpH;
    string tempStr;

    for(int e=0; e<Window[indexID].buttonCount; e++) {

        // relative Position
        tmpW = Window[indexID].button[e].w;
        tmpH = Window[indexID].button[e].h;

        //if(Window[indexID].button[e].align==0) { // left top
            tmpX = offsetX + Window[indexID].button[e].x;
            tmpY = offsetY + Window[indexID].button[e].y;
        //} else
        //if(Window[indexID].button[e].align==1) { // right bottom
        //    tmpX = offsetX + tmpW - Window[indexID].button[e].x;
        //    tmpY = offsetY + tmpH - Window[indexID].button[e].y;
        //}


        // left part
        blit(sfWindow,                  // source BITMAP
             Window[indexID].ptrBuffer, // dest BITMAP
             posWindow[style][11].x,    // source X
             posWindow[style][11].y,    // source Y
             tmpX,                      // dest X
             tmpY,                      // dest Y
             posWindow[style][11].w,    // general W
             posWindow[style][11].h);   // general H


        // middle part
        stretch_blit(sfWindow,                      // source BITMAP
                     Window[indexID].ptrBuffer,     // dest BITMAP
                     posWindow[style][12].x,        // source X
                     posWindow[style][12].y,        // source Y
                     posWindow[style][12].w,        // source W
                     posWindow[style][12].h,        // source H
                     tmpX+posWindow[style][11].w,   // dest X
                     tmpY,                          // dest Y
                     tmpW-posWindow[style][13].w,   // dest W
                     posWindow[style][11].h);       // dest H


        // right part
        blit(sfWindow,                          // source BITMAP
             Window[indexID].ptrBuffer,         // dest BITMAP
             posWindow[style][13].x,            // source X
             posWindow[style][13].y,            // source Y
             tmpX+tmpW-posWindow[style][11].w,  // dest X
             tmpY,                              // dest Y
             posWindow[style][13].w,            // general W
             posWindow[style][13].h);           // general H



        // draw content --- Align: Left=0, Centered=1, Right=2
        temp = tmpX+3; // Default left
        tempStr = Window[indexID].button[e].lable;
        if(Window[indexID].button[e].align==1) temp = tmpX+(Window[indexID].button[e].w/2)-(tempStr.length()*8/2);
        if(Window[indexID].button[e].align==2) temp = tmpX+Window[indexID].button[e].w-3-(tempStr.length()*8);
        textprintf_ex(Window[indexID].ptrBuffer, font, temp, tmpY+3, makecol(255, 255, 255), -1, "%s", tempStr.c_str() );
    }

*/
}





void windowManager::RenderImage(int indexID, int offsetX, int offsetY, int style) {

/*
    for(int e=0; e<Window[indexID].imageCount; e++) {

        if(Window[indexID].image[e].ptrImage != NULL) {


            blit(Window[indexID].image[e].ptrImage, // source BITMAP
                 Window[indexID].ptrBuffer,         // dest BITMAP
                 0,                                 // source X
                 0,                                 // source Y
                 Window[indexID].image[e].x,        // dest X
                 Window[indexID].image[e].y,        // dest Y
                 Window[indexID].image[e].w,        // general W
                 Window[indexID].image[e].h);       // general H
*/
             /*

        blit(Window[indexID].image[e].ptrImage, // source BITMAP
             Window[indexID].ptrBuffer,         // dest BITMAP
             0,                                 // source X
             0,                                 // source Y
             10,        // dest X
             10,        // dest Y
             80,        // general W
             80);       // general H
*/
/*

        //textprintf_ex(Window[indexID].ptrBuffer, font, Window[indexID].image[e].x, Window[indexID].image[e].y, makecol(255, 255, 255), -1, "%s", Window[indexID].image[e].source.c_str() );
        }
    }
*/
}




void windowManager::RenderText(int indexID, int offsetX, int offsetY, int style) {
/*
    int charCnt;
    int cBorder;
    int iX, iY;
    char tempChar;
    int tmpX, tmpY, tmpW, tmpH;
    string tempStr;

    int cx, cy, cw, ch; // content

    // Content - TEXT
    for(int e=0; e<Window[indexID].textCount; e++) {

        // Border
        if(Window[indexID].text[e].frame==0) cBorder = 0;
        else cBorder = Window[indexID].text[e].frame;

        // Relative align
        tmpX = offsetX + Window[indexID].text[e].x;
        tmpY = offsetY + Window[indexID].text[e].y;
        tmpW = Window[indexID].text[e].w;
        tmpH = Window[indexID].text[e].h;

        // Content space
        cx = tmpX + cBorder + 5;
        cy = tmpY + cBorder + 18;
        cw = Window[indexID].text[e].w - ( cBorder * 2 );
        ch = Window[indexID].text[e].h - ( cBorder * 2 );

        tempStr = breakLines(Window[indexID].text[e].content, cw);


        // Draw frame
        if(Window[indexID].text[e].frame > 0) {

            // left frame part
            stretch_blit(sfWindow,                  // source BITMAP
                         Window[indexID].ptrBuffer, // dest BITMAP
                         posWindow[style][18].x,    // source X
                         posWindow[style][18].y,    // source Y
                         posWindow[style][18].w,    // source W
                         posWindow[style][18].h,    // source H
                         cx-cBorder,                // dest X
                         cy-cBorder,                // dest Y
                         cBorder,                   // dest W
                         ch+(cBorder*2));           // dest H


            // right frame part
            stretch_blit(sfWindow,                  // source BITMAP
                         Window[indexID].ptrBuffer, // dest BITMAP
                         posWindow[style][18].x,    // source X
                         posWindow[style][18].y,    // source Y
                         posWindow[style][18].w,    // source W
                         posWindow[style][18].h,    // source H
                         cx+cw,                     // dest X
                         cy-cBorder,                // dest Y
                         cBorder,                   // dest W
                         ch+(cBorder*2));           // dest H


            // top frame part
            stretch_blit(sfWindow,                  // source BITMAP
                         Window[indexID].ptrBuffer, // dest BITMAP
                         posWindow[style][18].x,    // source X
                         posWindow[style][18].y,    // source Y
                         posWindow[style][18].w,    // source W
                         posWindow[style][18].h,    // source H
                         cx,                        // dest X
                         cy-cBorder,                // dest Y
                         cw,                        // dest W
                         cBorder);                  // dest H


            // bottom frame part
            stretch_blit(sfWindow,                  // source BITMAP
                         Window[indexID].ptrBuffer, // dest BITMAP
                         posWindow[style][18].x,    // source X
                         posWindow[style][18].y,    // source Y
                         posWindow[style][18].w,    // source W
                         posWindow[style][18].h,    // source H
                         cx,                        // dest X
                         cy+ch,                     // dest Y
                         cw,                        // dest W
                         cBorder);                  // dest H


            // content part
            stretch_blit(sfWindow,                  // source BITMAP
                         Window[indexID].ptrBuffer, // dest BITMAP
                         posWindow[style][17].x,    // source X
                         posWindow[style][17].y,    // source Y
                         posWindow[style][17].w,    // source W
                         posWindow[style][17].h,    // source H
                         cx,                        // dest X
                         cy,                        // dest Y
                         cw,                        // dest W
                         ch);                       // dest H

        }


        // Draw Content Lines
        iX = 0;
        iY = 0;
        for(charCnt=0;charCnt<(int)tempStr.length();charCnt++) {
            tempChar = tempStr.at(charCnt);
            //if( strcmp(&tempChar, "#") == NULL ) {
            if(strstr(&tempChar, "#")) {
                iY++;
                iX=0;
            } else {
                textprintf_ex(Window[indexID].ptrBuffer, font, cx+(iX*setupFontW), cy+(iY*setupFontH), makecol(255, 255, 255), -1, "%c", tempChar );
                iX++;
            }
        }

    }
*/
}


void windowManager::RenderWindow(int index) {
/*
    // Content - EDIT
    RenderEdit(index, x, y, style );

    // Content - BUTTONS
    RenderButton(index, x, y, style );

    // Content - TEXT
    RenderText(index, x, y, style );

    // Content - IMAGES
    RenderImage(index, x, y, style );
*/
}



void windowManager::Render(bool pInit) {

    int i;
    int index;

    for(i=(WindowCNT-1);i>=0;i--) {
        index = WindowLayer[i];
        //if( (Window[index].aktiv && Window[index].visible) || pInit) RenderWindow(index);
        if( (Window[index].aktiv && Window[index].visible) || pInit) Window[index].Render();
    }

}





void windowManager::Draw(void) {

    int i, index=0;

    // correct
    if(WindowResize != -1) Render(0);

    for(i=(WindowCNT-1);i>=0;i--) {
        index = WindowLayer[i];
        if(Window[index].aktiv && Window[index].visible) {
            if(Window[index].renderd == 0) Window[index].Render();

            // Window
            Window[index].Draw();

        }
        textprintf_ex(screen, font, 50, 300+(i*10), makecol(255, 255, 255), -1, "%s, i=%i", Window[index].title.c_str(), index);
    }


}





void windowManager::InjectMouse(int pMausX, int pMausY) {
    mouseX = pMausX;
    mouseY = pMausY;

}


void windowManager::InjectKeys(int pKey) {

    int index = WindowLayer[0];
    int AsciiCode = (pKey & 0xff);


    // hat Edits?
    if(Window[index].editCursor != -1) {

        switch(pKey >> 8) {

            case KEY_TAB:
                Window[index].editCursor++;
                if(Window[index].editCursor >= Window[index].editCount) Window[index].editCursor = 0;
            break;


            case KEY_ENTER:
                //if(Fenster[index].Handle == 1) winButtonKlick(1, 0);
            break;


            case KEY_BACKSPACE:
                if(Window[index].edit[Window[index].editCursor].cursorPos > 0) {
                    Window[index].edit[Window[index].editCursor].cursorPos--;
                    Window[index].edit[Window[index].editCursor].content.erase(Window[index].edit[Window[index].editCursor].cursorPos, 1);
                }
            break;


            case KEY_DEL:
                if((int)Window[index].edit[Window[index].editCursor].content.length() > Window[index].edit[Window[index].editCursor].cursorPos) {
                    Window[index].edit[Window[index].editCursor].content.erase(Window[index].edit[Window[index].editCursor].cursorPos, 1);
                }
            break;


            case KEY_LEFT:
                if(Window[index].edit[Window[index].editCursor].cursorPos > 0) Window[index].edit[Window[index].editCursor].cursorPos--;
            break;


            case KEY_RIGHT:
                if(Window[index].edit[Window[index].editCursor].cursorPos < Window[index].edit[Window[index].editCursor].maxChar-2 &&
                   Window[index].edit[Window[index].editCursor].cursorPos < (int)Window[index].edit[Window[index].editCursor].content.length()) Window[index].edit[Window[index].editCursor].cursorPos++;
            break;


            default:
                //if(isPlainChar(AsciiCode)) {
                    if(Window[index].edit[Window[index].editCursor].cursorPos < Window[index].edit[Window[index].editCursor].maxChar-2) {
                        Window[index].edit[Window[index].editCursor].content.insert(Window[index].edit[Window[index].editCursor].cursorPos, boost::lexical_cast<std::string>((char)AsciiCode));
                        Window[index].edit[Window[index].editCursor].cursorPos++;
                    }
                //}
            break;
        }


        RenderWindow(index);


    }


}


int windowManager::windowAdd(windows pWindow) {

    pWindow.handle = runningHandleNumber;


    pWindow.renderd = 0;
    //pWindow.ptrGrafics = myGrafics;






    // Layer
    WindowLayer.push_back(runningHandleNumber);   // Defines the window layer position of the window
    runningHandleNumber++;

    Window.push_back(pWindow);
    WindowCNT++;


    return runningHandleNumber-1;

}



int windowManager::windowGetNumber(void) {
    return WindowCNT;
}



int windowManager::windowGetFocused(void) {
    if(WindowCNT > 0) return WindowLayer[0];
    return -1;
}





int windowManager::windowGetIndexByHandle(int pHandle) {
    int i;
    for(i=0;i<WindowCNT;i++) {
        if(Window[i].handle == pHandle) return i;
    }

    cout << "windowGetIndexByHandle: No Handle found!" << endl;
    exit(2);
}

int windowManager::windowGetIndexById(int pId) {
    int i;
    for(i=0;i<WindowCNT;i++) {
        if(Window[i].id == pId) return i;
    }
    cout << "windowGetIndexById: No ID found!" << endl;
    exit(2);
}



int windowManager::windowGetLayerPosition(int pHandle) {
    int i;
    for(i=0;i<WindowCNT;i++) {
        if(WindowLayer[i] == pHandle) return i;
    }

    cout << "windowGetLayerPosition: No Handle found!" << endl;
    exit(2);
}



int windowManager::windowSetFocus(int pHandle) {

    if(WindowCNT > 1) { // Mehr als ein Fenster

        int i;
        int windowIndex = windowGetLayerPosition(pHandle);
        for(i=windowIndex;i>0;i--) WindowLayer[i] = WindowLayer[i-1];
        WindowLayer[0] = pHandle;

    }
    return 1;
}



void windowManager::windowOpen(int pHandle) {
    int tmpIndex = windowGetIndexByHandle(pHandle);
    Window[tmpIndex].aktiv = 1;
    windowSetFocus(tmpIndex);
}

void windowManager::windowClose(int pHandle) {
    int tmpIndex = windowGetIndexByHandle(pHandle);
    Window[tmpIndex].aktiv = 0;
}



void windowManager::windowClickHandler(int indexID, int objectID, int type) {

    int WindowID = Window[indexID].id;

    int btnHandle = 0;
    int imgHandle = 0;

    switch(type) {

        case 1: // Button
            btnHandle = Window[indexID].button[objectID].handle;
        break;


        case 2: // Image
            //imgHandle = Window[indexID].image[objectID].handle;
        break;

    }


    switch(WindowID) {

        case 1:

        break;


        case 2:

        break;


        case 3:

        break;


        case 4: // communication (CHAT)

        break;


        case 5:

        break;


        case 6:

        break;


        case 7: // Setup
            if(btnHandle==1) {
                //al_resize_display(800, 600);
                //set_gfx_mode(GFX_OPENGL, 1280, 1024, 0, 0);
            }
        break;


        case 8: // Login Dialog
            if(btnHandle==1) {
                net->sendMessage(3);  // LOGIN_ANSWER
            } else exit(2);
        break;


        case 9: // Select Server Dialog
            if(btnHandle==1) {

                net->Connect(Window[indexID].edit[0].content);
                Window[indexID].aktiv = 0;
            }
        break;

        case 10: // Character selection
            if(btnHandle==1) {
//                net->Connect(Window[indexID].edit[0].content);
//                Window[indexID].aktiv = 0;
            }

//            if(imgHandle==1) {
//                Window[indexID].image[0].visible = 0;
//            }

        break;


        default:
            cout << "windowClickHandler: no handling defined!" << endl;

        break;
    }

}






bool windowManager::windowClickContent(int pClickX, int pClickY, int indexID) {

    int tmpX, tmpY, tmpW, tmpH;
    string tempStr;


    // Button Handling
    for(int e=0; e<Window[indexID].buttonCount; e++) {

        // relative Position
        tmpX = Window[indexID].x + Window[indexID].button[e].x;
        tmpY = Window[indexID].y + Window[indexID].button[e].y;
        tmpW = Window[indexID].button[e].w;
        tmpH = Window[indexID].button[e].h;

        if(pClickX > tmpX && pClickX < (tmpX+tmpW) && pClickY > tmpY && pClickY < (tmpY+tmpH) ) windowClickHandler(indexID, e, 1);

    }

    // Image Handling
    /*
    for(int e=0; e<Window[indexID].imageCount; e++) {

        if(Window[indexID].image[e].clickable) {

            // relative Position
            tmpX = Window[indexID].x + Window[indexID].image[e].x;
            tmpY = Window[indexID].y + Window[indexID].image[e].y;
            tmpW = Window[indexID].image[e].w;
            tmpH = Window[indexID].image[e].h;

            if(pClickX > tmpX && pClickX < (tmpX+tmpW) && pClickY > tmpY && pClickY < (tmpY+tmpH) ) windowClickHandler(indexID, e, 2);
        }

    }
    */

    return true;
}


string windowManager::windowGetEdit(int pWindowID, int pButtonID) {
    return Window[windowGetIndexByHandle(pWindowID)].edit[pButtonID].content;
}


bool windowManager::windowClick(int pClickX, int pClickY) {

    int i, x, y, w, h;
    int Focus = -1;
    int Close = -1;
    bool retVal = 0;

    for(i=WindowCNT;i>=0;i--) {
        if(Window[i].aktiv) {

            x = Window[i].x;
            y = Window[i].y;
            w = Window[i].w;
            h = Window[i].h;

            // Generell
            if(pClickX > x && pClickX < (x+w) && pClickY > y && pClickY < (y+h)) {
                Focus = Window[i].handle;
                Close = -1;
            }

            // Schliessen
            if(pClickX > (x+w-15) && pClickX < (x+w) && pClickY > y && pClickY < (y+15)) {
                Close = Window[i].handle;
            }

        }
    }
    // Focus ueberschreibt sich in reihenfolge der ebenen
    // Wenn ein flaechenklick nach irgendeinem menueclick kommt nur aktiv
    if(Close!=-1) {
        int tmpIndex = windowGetIndexByHandle(Close);
        if(Window[tmpIndex].menue) Window[tmpIndex].aktiv = 0;
        retVal = 1;
    }
    else if(Focus!=-1) {
        windowSetFocus(Focus);
        windowClickContent(pClickX, pClickY, Focus);
        retVal = 1;
    }
    return retVal;
}



void windowManager::windowMove(int pDiffX, int pDiffY, int pMausX, int pMausY) {


    int i, x, y, w, h;
    int flagMove = -1, flagResize = -1;
    if(WindowMove == -1 && WindowResize == -1) {
        for(i=0;i<WindowCNT;i++) {
            if(Window[i].aktiv) {


                x = Window[i].x;
                y = Window[i].y;
                w = Window[i].w;
                h = Window[i].h;



                // Klick in Flaeche?
                if(pMausX > x && pMausX < (x+w) && pMausY > (y+15) && pMausY < (y+h)) {
                    flagMove   = -1;
                    flagResize = -1;
                }



                // Klick in Resize?
                if(Window[i].resizable){
                    if(pMausX > (x+w-15) && pMausX < (x+w) && pMausY > (y+h-15) && pMausY < (y+h)) {
                        flagResize = Window[i].handle;
                    }
                }



                // Klick in Topleiste?
                if(Window[i].movable){
                    if(pMausX > x && pMausX < (x+w) && pMausY > y && pMausY < (y+15)) {
                        flagMove = Window[i].handle;
                    }
                }


            }
        }

        // Focus ueberschreibt sich in reihenfolge der ebenen
        if(flagMove != -1) {
            windowSetFocus(flagMove);
            WindowMove = flagMove;
            WindowTempX = Window[windowGetIndexByHandle(flagMove)].x - pMausX;
            WindowTempY = Window[windowGetIndexByHandle(flagMove)].y - pMausY;
        } else
        if(flagResize != -1) {
            WindowResize = flagResize;
            windowSetFocus(flagResize);
            WindowTempX = Window[windowGetIndexByHandle(flagResize)].w;
            WindowTempY = Window[windowGetIndexByHandle(flagResize)].h;
        }


    } else {


        if(WindowMove != -1) {
            Window[windowGetIndexByHandle(WindowMove)].x = pMausX + pDiffX + WindowTempX;
            Window[windowGetIndexByHandle(WindowMove)].y = pMausY + pDiffY + WindowTempY;
        } else
        if(WindowResize != -1) {


            int a,b;
            a = WindowTempX + pDiffX;
            b = WindowTempY + pDiffY;

            if(a > 200) Window[windowGetIndexByHandle(WindowResize)].w = a;
            else Window[windowGetIndexByHandle(WindowResize)].w = 200;
            if(b >  100) Window[windowGetIndexByHandle(WindowResize)].h = b;
            else Window[windowGetIndexByHandle(WindowResize)].h = 100;
        }


    }
}



void windowManager::windowClickRelease() {

    WindowMove = -1;
    WindowResize = -1;
    WindowTempX=0;
    WindowTempY=0;

}

